package com.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

/**
 * 消费者
 */
@Slf4j
@Component
public class SpringRabbitListener {
    // 利用RabbitListener来声明要监听的队列信息
    // 将来一旦监听的队列中有了消息，就会推送给当前服务，调用当前方法，处理消息。
    // 可以看到方法体中接收的就是消息体的内容
    @RabbitListener(queues = "simple.queue")
    public void listenerSimpleQueueMessage(String message){
        System.out.println("spring 消费者接收到消息：【" + message + "】");
        if(true){
            throw new RuntimeException("故意的");
        }
        log.info("消息处理完成");
    }
    @RabbitListener(queues = "work.queue")
    public void listenerWorkQueueMessage1(String message) throws InterruptedException {
        log.info("消费者1接收到消息：【" + message + "】" + LocalTime.now());
        Thread.sleep(20); //相当于每秒钟处理50个消息
    }
    @RabbitListener(queues = "work.queue")
    public void listenerWorkQueueMessage2(String message) throws InterruptedException {
        log.error("消费者2接收到消息：【" + message + "】" + LocalTime.now());
        Thread.sleep(200); //相当于每秒钟处理5个消息
    }
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) throws InterruptedException {
        log.error("消费者1接收到消息：【" + message + "】");
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) throws InterruptedException {
        log.error("消费者2接收到消息：【" + message + "】");
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(Map<String, Object> message) throws InterruptedException {
        log.info("消费者1接收到消息：【" + message + "】");
    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "direct.queue2"),
//            exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
//            key = {"red","yellow"}
//    ))
//    public void listenDirectQueue2(String message) throws InterruptedException {
//        log.error("消费者2接收到消息：【" + message + "】");
//    }
    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg){
        System.out.println("消费者1接收到topic.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg){
        System.out.println("消费者2接收到topic.queue2的消息：【" + msg + "】");
    }
//    自从3.12版本后，默认创建的队列就是Lazy队列，所以这里就不用声明了
//    @RabbitListener(queuesToDeclare = @Queue(
//            name = "lazy.queue",
//            durable = "true",
//            arguments = @Argument(name = "x-queue-type",value = "lazy")
//    ))
//    public void listenLazyQueue(String message){
//        log.info("消费者1接收到消息：【" + message + "】");
//    }
}
